// pages/user/index.js
Page({
    data: {
        userInfo: {},
        // 被收藏的商品的数量
        collectNums: 0,
        hasLogin: false,
        icons:[
            "/icons/waitPay.png",
            "../../icons/waitSend.png",
            "../../icons/waitRecive.png",
            "../../icons/done.png"
        ]
    },
    toSet(){
        wx.navigateTo({
            url:'../set/index'
        })
    },
    onLoad(){

    },
    toOrderList(){
        wx.navigateTo({
            url:'../orderList/index'
        })
    },
    onShow() {
        let time = new Date().getTime()
        let begintime = wx.getStorageSync("begintime")
        if(time-begintime>=3600000){
            this.setData({
                hasLogin:false
            })
        }else {
            this.setData({
                hasLogin:true,
                userInfo:wx.getStorageSync("userInfo").userInfo
            })
        }
        // const collect = wx.getStorageSync("collect") || [];

    },
    login() {
        let that = this
        wx.login({
            success(res) {
                let code = res.code
                wx.showModal({
                    title: "",
                    content: '请先登录!',
                    success(res) {
                        if(res.confirm){
                            wx.getUserProfile({
                                desc: '用于完善用户资料',
                                success(response) {
                                    wx.setStorageSync('userInfo', {
                                        userInfo: response.userInfo
                                    })
                                    let iv = response.iv
                                    let encryptedData = response.encryptedData
                                    that.setData({
                                        userInfo: response.userInfo,
                                    })
                                    wx.request({
                                        url: 'https://www.cbing.icu:8095/login',
                                        method: 'POST',
                                        data: {
                                            encryptedData: encryptedData,
                                            iv: iv,
                                            code: code
                                        },
                                        success(response3) {
                                            that.setData({
                                                hasLogin:true
                                            })
                                            wx.setStorageSync('token', response3.data.data.token)
                                            wx.setStorageSync("begintime",new Date().getTime())
                                        },
                                        fail(response3) {
                                        }
                                    })
                                }
                            })
                        }

                    }
                })

            }
        })

    },
    toAddress(){
        if(this.data.hasLogin===true){
            wx.navigateTo({
                url:'../address/index'
            })
        }else {
            wx.showToast({
                title:"请先登录！",
                duration:2000,
                icon:"none"
            })
        }

    }
})